from playwright.sync_api import sync_playwright
from auth import create_context_with_session, get_session_path
from page.xianlufeiyongPage import customer_line
from config import BASE_URL


def main():
    # 显示会话文件路径
    session_path = get_session_path()
    print(f"会话文件路径: {session_path}")
    # 只创建一个Playwright实例
    with sync_playwright() as playwright:
        # 创建带有会话的浏览器上下文
        context, browser = create_context_with_session(playwright)
        page = context.new_page()

        try:
            # 直接访问应用主页
            print("导航到应用主页...")
            page.goto(f"{BASE_URL}/#/twms/client/clientcost")

            # 检查是否已登录
            try:
                page.wait_for_selector("div:has-text('客户管理中心')", timeout=10000)
                print("成功跳过登录，使用保存的会话")

                # 执行业务操作
                customer_line(page)
                print("操作完成")

            except Exception as e:
                print(f"会话验证失败: {e}")
                print("请检查登录状态或重新运行程序")

        except Exception as e:
            print(f"操作过程中出错: {e}")

        finally:
            # 确保浏览器正确关闭
            page.close()
            context.close()
            browser.close()


if __name__ == "__main__":
    main()